package ltd2001.moneyuse;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.String;

public class MainActivity extends Activity {
	
	public String RECORD = "RECORD";
	public int lastMoney;
//	public int lastFiveMoney1;
//	public int lastFiveMoney2;
//	public int lastFiveMoney3;
//	public int lastFiveMoney4;
//	public int lastFiveMoney5;
	public int recordOrder;
	public int recordNumber;
	public int recordAnnotate;
//	public int test = 0;
	public boolean radioChecked = false;
	public Button addContext;
	public Button exitButton;
	public Button resetButton;
	public Button readInButton;
	public TextView lastMoneyTextView;
	public TextView context1;
	public TextView context2;
	public TextView context3;
	public TextView context4;
	public TextView context5;
	public RadioGroup mRadioGroup;
	public RadioButton mRadioButton0;
	public RadioButton mRadioButton1;
	public EditText textNumberEditText;
	public EditText textAnnotateEditText;
	
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        /*lock orientation*/
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        /*find all parameters' IDs*/
        addContext = (Button) findViewById(R.id.button1);
        exitButton = (Button) findViewById(R.id.button2);
        resetButton = (Button) findViewById(R.id.button3);
        readInButton = (Button) findViewById(R.id.button4);
        lastMoneyTextView = (TextView) findViewById(R.id.textView1);
        context1 = (TextView) findViewById(R.id.textView3);
        context2 = (TextView) findViewById(R.id.textView4);
        context3 = (TextView) findViewById(R.id.textView5);
        context4 = (TextView) findViewById(R.id.textView6);
        context5 = (TextView) findViewById(R.id.textView7);
        mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
        mRadioButton0 = (RadioButton) findViewById(R.id.radio0);
        mRadioButton1 = (RadioButton) findViewById(R.id.radio1);
        textNumberEditText = (EditText) findViewById(R.id.editText1);
        textAnnotateEditText = (EditText) findViewById(R.id.editText2);
        /*find all parameters' IDs END*/
        
        //read in data
        SharedPreferences mPreferences = getPreferences(0);
        refreshFiveLastMoney(mPreferences.getInt(RECORD, 0) - 1);
        if(!(mPreferences.contains("lastMoney"))){
        	lastMoney = 0;
        }
        else {
			lastMoney = mPreferences.getInt("lastMoney", 99999);
		}
        
        
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){//Radiogroup setting
			public void onCheckedChanged(RadioGroup group, int checkedId){
				if(checkedId == mRadioButton0.getId()){
					radioChecked = false;//income
//					lastMoneyTextView.setText("radioChecked = "+ test + test++ +" "+ test);
				}
				else if(checkedId == mRadioButton1.getId()){
					radioChecked = true;//outcome
//					lastMoneyTextView.setText("radioChecked = "+ test + ++test +" "+test);
				}
			}});//Radiogroup setting end
        
        
        addContext.setOnClickListener(new Button.OnClickListener(){//button addContext setting
			public void onClick(View v) {
				
				//get string from EditText
				String inputAnnotate = textAnnotateEditText.getText().toString();
				String inputNumber = textNumberEditText.getText().toString();
				
				if (inputNumber.matches("")) {
					Toast.makeText(MainActivity.this, "Please input number.", Toast.LENGTH_SHORT).show();
				}
				
				else if(inputAnnotate.matches("")){
					Toast.makeText(MainActivity.this, "Please input annotate.", Toast.LENGTH_SHORT).show();
				}
				
				else {
				
					SharedPreferences settings = getPreferences(0);
					recordOrder = settings.getInt(RECORD, 0);
				    SharedPreferences.Editor editor = settings.edit();
				    if(radioChecked == false){
				    	lastMoney = //lastMoney add whit inputNumber
				    	lastMoney + Integer.valueOf(textNumberEditText.getText().toString());
				    	
				    	editor.putInt(Integer.toString(recordOrder)+".number",
				    				  Integer.valueOf(textNumberEditText.getText().toString()));
				    }//income, put positive number
				    
				    else if (radioChecked == true) {
				    	lastMoney = //lastMoney minus with inputNumber
				    	lastMoney - Integer.valueOf(textNumberEditText.getText().toString());
				    	
				    	editor.putInt(Integer.toString(recordOrder)+".number",
			    				 	  0 - Integer.valueOf(textNumberEditText.getText().toString()));
					}//outcome, put negative number
				    
				    editor.putString(Integer.toString(recordOrder)+".annotate",
				    				 textAnnotateEditText.getText().toString());
				    recordOrder = recordOrder + 1;
				    editor.putInt(RECORD, recordOrder);
				    editor.putInt("lastMoney", lastMoney);
				    editor.commit();
//				clear EditText context
				textNumberEditText.setText("");
				textAnnotateEditText.setText("");
//				refresh display function
				refreshFiveLastMoney(settings.getInt(RECORD, 0) - 1);
				

				}//check EditText is empty end
				
			}});//button addContext setting end
        
        exitButton.setOnClickListener(new Button.OnClickListener(){//button exit setting
			public void onClick(View v) {
				
				finish();
			}});//button exit end
        
        resetButton.setOnClickListener(new Button.OnClickListener(){
        		public void onClick(View v) {
				
        		SharedPreferences reset = getPreferences(0);
				SharedPreferences.Editor editor = reset.edit();
				editor.clear();
				editor.commit();
				lastMoney = 0;
				refreshFiveLastMoney(reset.getInt(RECORD, 0) - 1);
			}});//button reset end
        
        readInButton.setOnClickListener(new Button.OnClickListener(){//Only for test
    		public void onClick(View v) {
    			SharedPreferences mPreferences = getPreferences(0);
    			refreshFiveLastMoney(mPreferences.getInt(RECORD, 0) - 1);
    			
		}});//readIn button end
        
        
    }//OnCreat
    
    private void refreshFiveLastMoney(int recordOrder){//refresh & display function
    	
    	SharedPreferences mPreferences = getPreferences(0);
    	
    	lastMoneyTextView.setText("剩下金額： " + Integer.toString(mPreferences.getInt("lastMoney", 99999)));
    	
    	if (!(mPreferences.contains("0.number") || mPreferences.contains("0.annotate"))) {
			context1.setText("No Data");
			context2.setText("");
			context3.setText("");
			context4.setText("");
			context5.setText("");
			}
    	else{
    		if(recordOrder == 0){
    			context1.setText("第" + (recordOrder+1) + "次: " + Integer.toString(mPreferences.getInt(Integer.toString(recordOrder)+".number", 99999)) + " " + 
								mPreferences.getString(Integer.toString(recordOrder)+".annotate", "#####"));
    		}
    		else if (recordOrder == 1) {
    			context1.setText("第" + (recordOrder) + "次: " + Integer.toString(mPreferences.getInt(Integer.toString(recordOrder - 1)+".number", 99999)) + " " + 
						mPreferences.getString(Integer.toString(recordOrder - 1)+".annotate", "#####"));
    			context2.setText("第" + (recordOrder+1) + "次: " + Integer.toString(mPreferences.getInt(Integer.toString(recordOrder)+".number", 99999)) + " " + 
						mPreferences.getString(Integer.toString(recordOrder)+".annotate", "#####"));
			}
    		else if (recordOrder == 2) {
    			context1.setText("第" + (recordOrder - 1) + "次: " + Integer.toString(mPreferences.getInt(Integer.toString(recordOrder - 2)+".number", 99999)) + " " + 
						mPreferences.getString(Integer.toString(recordOrder - 2)+".annotate", "#####"));
    			context2.setText("第" + (recordOrder) + "次: " + Integer.toString(mPreferences.getInt(Integer.toString(recordOrder - 1)+".number", 99999)) + " " + 
						mPreferences.getString(Integer.toString(recordOrder - 1)+".annotate", "#####"));
    			context3.setText("第" + (recordOrder + 1) + "次: " + Integer.toString(mPreferences.getInt(Integer.toString(recordOrder)+".number", 99999)) + " " + 
						mPreferences.getString(Integer.toString(recordOrder)+".annotate", "#####"));
			}
    		else if (recordOrder == 3) {
    			context1.setText("第" + (recordOrder - 2) + "次: " + Integer.toString(mPreferences.getInt(Integer.toString(recordOrder - 3)+".number", 99999)) + " " + 
						mPreferences.getString(Integer.toString(recordOrder - 3)+".annotate", "#####"));
    			context2.setText("第" + (recordOrder - 1) + "次: " + Integer.toString(mPreferences.getInt(Integer.toString(recordOrder - 2)+".number", 99999)) + " " + 
						mPreferences.getString(Integer.toString(recordOrder - 2)+".annotate", "#####"));
    			context3.setText("第" + (recordOrder) + "次: " + Integer.toString(mPreferences.getInt(Integer.toString(recordOrder - 1)+".number", 99999)) + " " + 
						mPreferences.getString(Integer.toString(recordOrder - 1)+".annotate", "#####"));
    			context4.setText("第" + (recordOrder + 1) + "次: " + Integer.toString(mPreferences.getInt(Integer.toString(recordOrder)+".number", 99999)) + " " + 
						mPreferences.getString(Integer.toString(recordOrder)+".annotate", "#####"));
			}
    		else{
    			context1.setText("第" + (recordOrder - 3) + "次: " + Integer.toString(mPreferences.getInt(Integer.toString(recordOrder - 4)+".number", 99999)) + " " + 
						mPreferences.getString(Integer.toString(recordOrder - 4)+".annotate", "#####"));
    			context2.setText("第" + (recordOrder - 2) + "次: " + Integer.toString(mPreferences.getInt(Integer.toString(recordOrder - 3)+".number", 99999)) + " " + 
						mPreferences.getString(Integer.toString(recordOrder - 3)+".annotate", "#####"));
    			context3.setText("第" + (recordOrder - 1) + "次: " + Integer.toString(mPreferences.getInt(Integer.toString(recordOrder - 2)+".number", 99999)) + " " + 
						mPreferences.getString(Integer.toString(recordOrder - 2)+".annotate", "#####"));
    			context4.setText("第" + (recordOrder) + "次: " + Integer.toString(mPreferences.getInt(Integer.toString(recordOrder - 1)+".number", 99999)) + " " + 
						mPreferences.getString(Integer.toString(recordOrder - 1)+".annotate", "#####"));
    			context5.setText("第" + (recordOrder + 1) + "次: " + Integer.toString(mPreferences.getInt(Integer.toString(recordOrder)+".number", 99999)) + " " + 
						mPreferences.getString(Integer.toString(recordOrder)+".annotate", "#####"));
    		}
    	}//check element is empty end
    	
    }//refresh last five money use end
    
}//Activity

